//
//  CYUpgradeView.m
//  cyjf
//
//  Created by tauCross on 2017/6/1.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYUpgradeView.h"



@interface CYUpgradeView ()

@property(nonatomic, strong)UIView *maskView;
@property(nonatomic, strong)UIView *contentView;

@property(nonatomic, strong)NSString *url;

@end


@implementation CYUpgradeView

- (void)dealloc
{
    
}

- (instancetype)initWithTitle:(NSString *)title type:(CYUpgradeViewType)type texts:(NSArray<NSString *> *)texts url:(NSString *)url
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if(self)
    {
        
        self.url = url;
        
        self.maskView = [[UIView alloc] initWithFrame:self.bounds];
        self.maskView.alpha = 0;
        self.maskView.backgroundColor = HEXCOLORA(@"000000", 0.5);
        [self addSubview:self.maskView];
        
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 295, 295)];
        self.contentView.bottom = 0;
        self.contentView.centerX = self.width / 2;
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.contentView.layer.masksToBounds = YES;
        self.contentView.layer.cornerRadius = 4;
        [self addSubview:self.contentView];
        {
            UIImageView *topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.width, 102)];
            topImageView.image = CYImage(@"upgrade_top_image");
            topImageView.left = 0;
            topImageView.top = 0;
            [self.contentView addSubview:topImageView];
            
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.text = title;
            titleLabel.textColor = HEXCOLOR(@"444444");
            titleLabel.font = [UIFont systemFontOfSize:16];
            [titleLabel sizeToFit];
            titleLabel.centerX = self.contentView.width / 2;
            titleLabel.centerY = topImageView.bottom + 28;
            [self.contentView addSubview:titleLabel];
            
            NSArray *showTexts = texts.count > 3 ? [texts subarrayWithRange:NSMakeRange(0, 3)] : texts;
            CGFloat y = topImageView.bottom + 71;
            for(NSString *text in showTexts)
            {
                UIView *dot = [[UIView alloc] init];
                dot.width = 6;
                dot.height = 6;
                dot.layer.masksToBounds = YES;
                dot.layer.cornerRadius = 3;
                dot.backgroundColor = HEXCOLORA(@"ECECEC", 0.99);
                dot.centerY = y;
                dot.left = 20;
                [self.contentView addSubview:dot];
                
                UILabel *textLabel = [[UILabel alloc] init];
                textLabel.text = text;
                textLabel.font = [UIFont systemFontOfSize:14];
                textLabel.textColor = HEXCOLOR(@"666666");
                [textLabel sizeToFit];
                textLabel.width = 239;
                textLabel.centerY = y;
                textLabel.left = 36;
                [self.contentView addSubview:textLabel];
                
                y += 21;
            }
            
            UIView *bottomLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
            bottomLine.width = self.contentView.width;
            bottomLine.height = 1 / SCREEN_SCALE;
            bottomLine.left = 0;
            bottomLine.top = self.contentView.height - 44;
            [self.contentView addSubview:bottomLine];
            
            UIButton *upgradeButton = [[UIButton alloc] init];
            [upgradeButton setTitleColor:APP_DEFAULT_COLOR_BLUE forState:UIControlStateNormal];
            upgradeButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
            [upgradeButton addTarget:self action:@selector(onUpgradeButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:upgradeButton];
            
            switch(type)
            {
                case CYUpgradeViewTypeOption:
                {
                    UIView *mLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
                    mLine.width = 1 / SCREEN_SCALE;
                    mLine.height = 26;
                    mLine.centerX = self.contentView.width / 2;
                    mLine.centerY = self.contentView.height - 22;
                    [self.contentView addSubview:mLine];
                    
                    UIButton *ignoreButton = [[UIButton alloc] init];
                    ignoreButton.width = self.contentView.width / 2;
                    ignoreButton.height = 44;
                    ignoreButton.left = 0;
                    ignoreButton.bottom = self.contentView.height;
                    [ignoreButton setTitle:@"稍后再说" forState:UIControlStateNormal];
                    [ignoreButton setTitleColor:HEXCOLOR(@"444444") forState:UIControlStateNormal];
                    ignoreButton.titleLabel.font = [UIFont systemFontOfSize:16];
                    [ignoreButton addTarget:self action:@selector(onIgnoreButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
                    [self.contentView addSubview:ignoreButton];
                    
                    [upgradeButton setTitle:@"立即安装" forState:UIControlStateNormal];
                    upgradeButton.frame = ignoreButton.frame;
                    upgradeButton.left = ignoreButton.right;
                    
                    break;
                }
                case CYUpgradeViewTypeRequire:
                {
                    [upgradeButton setTitle:@"现在升级" forState:UIControlStateNormal];
                    upgradeButton.width = self.contentView.width;
                    upgradeButton.height = 44;
                    upgradeButton.left = 0;
                    upgradeButton.bottom = self.contentView.height;
                    
                    break;
                }
            }
        }
    }
    return self;
}

#pragma mark - Action
- (void)show
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    APP_LOCK_USER_INTERACTION
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.maskView.alpha = 1;
                         self.contentView.centerY = self.height / 2;
                     }
                     completion:^(BOOL finished) {
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

- (void)close
{
    APP_LOCK_USER_INTERACTION
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.maskView.alpha = 0;
                         self.contentView.top = self.height;
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

#pragma mark - Control Events
- (void)onIgnoreButtonTouchUpInside
{
    [self close];
}

- (void)onUpgradeButtonTouchUpInside
{
    [[UIApplication sharedApplication] openURL:self.url.tc_url];
}

@end
